import cv2

image_filename = '1-1b_rotated_columns-c.jpg'
img = cv2.imread(image_filename, cv2.IMREAD_GRAYSCALE)

cv2.imshow("gray", img)
# 均值法
th1 = cv2.adaptiveThreshold(img, 255,
                            cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY, 25, 20)

# 高斯法
th2 = cv2.adaptiveThreshold(img, 255,
                            cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                            cv2.THRESH_BINARY, 25, 20)
cv2.imwrite(image_filename.replace('.jpg', '_th1.jpg'),th1)
cv2.imwrite(image_filename.replace('.jpg', '_th2.jpg'), th2)
cv2.imshow('Mean', th1)
cv2.imshow('Gaussian', th2)
cv2.waitKey(0)